//
//  HouseDetailViewController.m
//  YJRenter
//
//  Created by 文彬 on 2018/7/2.
//  Copyright © 2018年 文彬. All rights reserved.
//

#import "HouseDetailViewController.h"
#import "ItemSelectTableViewCell.h"
#import "HouseDetailNavView.h"
#import "HouseDetailHeaderView.h"
#import "HouseDetailUserInfoTableViewCell.h"
#import "HouseDetailBottomView.h"
#import "LandlordHomeViewController.h"
#import "MakeOrderViewController.h"
#import "FacilityViewController.h"
#import "HomeRuleViewController.h"

@interface HouseDetailViewController ()<UITableViewDataSource,
    UITableViewDelegate,
    HouseDetailNavViewDelegate,
    HouseDetailBottomViewDelegate>

@property (strong, nonatomic) UITableView *listTableView;
@property (strong, nonatomic) HouseDetailNavView *navView;
@property (strong, nonatomic) HouseDetailHeaderView *headerView;
@property (strong, nonatomic) HouseDetailBottomView *bottomView;

@property (strong, nonatomic) NSArray *itemDatas;

@end

@implementation HouseDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self initPageControl];
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:YES animated:animated];
}

- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [self.navigationController setNavigationBarHidden:NO animated:animated];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/
- (void)initPageControl{
    
    [self.view addSubview:self.listTableView];
    [self.listTableView makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.right.equalTo(@0);
        make.bottom.equalTo(@-50);
    }];
    self.listTableView.tableHeaderView = self.headerView;
    
    [self.view addSubview:self.navView];
    
    [self.view addSubview:self.bottomView];
}

#pragma mark HouseFilterViewDelegate
- (void)houseDetailNavClick:(NSInteger)index{
    if (index==10) {
        [self.navigationController popViewControllerAnimated:YES];
    }
}

#pragma mark HouseDetailBottomViewDelegate
- (void)HouseDetailBottomViewClickHandle:(HouseDetailBottomViewClickType)type{

    if (type==kHouseDetailBottomViewClickTypeMakeOrder) { //立即预定
        
        MakeOrderViewController *makeOrderController = [[MakeOrderViewController alloc]init];
        [self.navigationController pushViewController:makeOrderController animated:YES];
    }
}
#pragma mark ScrollView
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    
    [self.navView scrollOffset:scrollView.contentOffset.y];
}

#pragma mark UITableView
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 4;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 10;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section==0||indexPath.section==1) {
        return 110;
    }
    return 60;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if(indexPath.section==0){
        
        ItemSelectTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ItemSelectTableViewCell" forIndexPath:indexPath];
        [cell setVerticalSpace:20];
        [cell setData:self.itemDatas oneLineCount:5];
        WeakObj(self)
        cell.itemClicked = ^(id param) {
            if ([param intValue]==4) {
                FacilityViewController *facilityController = [[FacilityViewController alloc]init];
                [selfWeak.navigationController presentViewController:facilityController animated:YES completion:nil];
            }
        };
        return cell;
    }
    else if(indexPath.section==1){
        
         HouseDetailUserInfoTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"HouseDetailUserInfoTableViewCell" forIndexPath:indexPath];
        [cell.imgView  setRoundImageWithURL:@"https://ss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u=4036889747,3526425271&fm=27&gp=0.jpg" placeholderImage:nil];
        return cell;
    }
    
    
    static NSString *iden = @"iden";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:iden];
    if (cell==nil) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:iden];
    }
    if (indexPath.section==2) {
        cell.textLabel.text = @"客房守则";
    }
    else if(indexPath.section==3){
        cell.textLabel.text = @"房屋详情";
    }
    
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    if (indexPath.section==1) { //房东主页
        LandlordHomeViewController *homeController = [[LandlordHomeViewController alloc]init];
        [self.navigationController pushViewController:homeController animated:YES];
    }
    else if(indexPath.section==2){ //客房守则
        HomeRuleViewController *ruleController = [[HomeRuleViewController alloc]init];
        [self presentViewController:ruleController animated:YES completion:nil];
    }
}

#pragma mark getter
- (UITableView*)listTableView{
    if (_listTableView==nil) {
        _listTableView = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStyleGrouped];
        _listTableView.delegate = self;
        _listTableView.dataSource = self;
        _listTableView.sectionFooterHeight = 0;
        _listTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _listTableView.backgroundColor = [UIColor clearColor];
        _listTableView.separatorStyle = UITableViewCellSeparatorStyleNone;        [_listTableView registerNib:[UINib nibWithNibName:@"ItemSelectTableViewCell" bundle:nil] forCellReuseIdentifier:@"ItemSelectTableViewCell"];
        [_listTableView registerNib:[UINib nibWithNibName:@"HouseDetailUserInfoTableViewCell" bundle:nil] forCellReuseIdentifier:@"HouseDetailUserInfoTableViewCell"];
        
    }
    
    return _listTableView;
}
- (HouseDetailNavView*)navView{
    if (_navView==nil) {
        _navView = [[HouseDetailNavView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 64)];
        _navView.delegate = self;
    }
    return _navView;
}
- (HouseDetailHeaderView*)headerView{
    if (_headerView==nil) {
        _headerView = [[HouseDetailHeaderView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 300)];
    }
    return _headerView;
}

- (HouseDetailBottomView*)bottomView{
    if (_bottomView==nil) {
        _bottomView = [[HouseDetailBottomView alloc]initWithFrame:CGRectMake(0, SCREEN_HEIGHT-50, SCREEN_WIDTH, 50)];
        _bottomView.delegate = self;
    }
    return _bottomView;
}

-(NSArray*)itemDatas{
    if (_itemDatas==nil) {
        _itemDatas  = @[@{@"title":@"6人",@"pic":@"header_default"},
                        @{@"title":@"70m",@"pic":@"header_default"},
                        @{@"title":@"3室",@"pic":@"header_default"},
                        @{@"title":@"3床",@"pic":@"header_default"},
                        @{@"title":@"更多",@"pic":@"header_default"}];
    }
    return _itemDatas;
}


@end
